
SHELL=/bin/bash

CFLAGS := -g -Wall -std=gnu99 -DDEMO_APP_FOR_GCC -fpack-struct=1
CC=gcc
AR=ar

# include
INC_SHARE 			:= -I./shared/include

INC_EVCC  			:= $(INC_SHARE) -I./evcc/include
INC_SECC  			:= $(INC_SHARE) -I./secc/include

INC_FILE_SHARE 		:= $(wildcard shared/include/*.h)
INC_FILE_SHARE 		+= $(wildcard shared/src/*.h)

INC_FILE_EVCC_LIB  	:= $(INC_FILE_SHARE)
INC_FILE_EVCC_LIB  	+= $(wildcard evcc/include/*.h)
INC_FILE_EVCC_LIB  	+= $(wildcard evcc/gbt27930/*.h)
INC_FILE_EVCC_LIB  	+= $(wildcard evcc/utility/*.h)

INC_FILE_EVCC_APP  	:= $(INC_FILE_EVCC_LIB)
INC_FILE_EVCC_APP  	+= $(wildcard evcc/app/*.h)
INC_FILE_EVCC_APP  	+= $(wildcard evcc/hal/*.h)
INC_FILE_EVCC_APP  	+= $(wildcard x86/*.h)
INC_FILE_EVCC_APP  	+= $(wildcard x86/evcc/*.h)

INC_FILE_SECC_LIB  	:= $(INC_FILE_SHARE)
INC_FILE_SECC_LIB  	+= $(wildcard secc/include/*.h)
INC_FILE_SECC_LIB  	+= $(wildcard secc/gbt27930/*.h)
INC_FILE_SECC_LIB  	+= $(wildcard secc/utility/*.h)

INC_FILE_SECC_APP  	:= $(INC_FILE_SECC_LIB)
INC_FILE_SECC_APP  	+= $(wildcard secc/app/*.h)
INC_FILE_SECC_APP  	+= $(wildcard secc/hal/*.h)
INC_FILE_SECC_APP  	+= $(wildcard x86/*.h)
INC_FILE_SECC_APP  	+= $(wildcard x86/secc/*.h)

# source
SRC_SHARE           := $(wildcard shared/src/*.c)

SRC_EVCC_LIB        := $(SRC_SHARE)
SRC_EVCC_LIB 		+= $(wildcard evcc/gbt27930/*.c)
SRC_EVCC_LIB 		+= $(wildcard evcc/utility/*.c)

SRC_EVCC_APP 		:= $(wildcard evcc/hal/*.c)
SRC_EVCC_APP 		+= $(wildcard evcc/app/*.c)
SRC_EVCC_APP 		+= $(wildcard x86/*.c)
SRC_EVCC_APP 		+= $(wildcard x86/evcc/*.c)

SRC_SECC_LIB 		:= $(SRC_SHARE)
SRC_SECC_LIB 		+= $(wildcard secc/gbt27930/*.c)
SRC_SECC_LIB 		+= $(wildcard secc/utility/*.c)

SRC_SECC_APP 		:= $(wildcard secc/hal/*.c)
SRC_SECC_APP 		+= $(wildcard secc/app/*.c)
SRC_SECC_APP 		+= $(wildcard x86/*.c)
SRC_SECC_APP 		+= $(wildcard x86/secc/*.c)

# object
OBJ_EVCC_DIR		:= ./evcc/obj
OBJ_SECC_DIR 		:= ./secc/obj

OBJ_EVCC_LIB 		:= $(patsubst %.c,$(OBJ_EVCC_DIR)/%.o,$(SRC_EVCC_LIB))
OBJ_EVCC_APP 		:= $(patsubst %.c,$(OBJ_EVCC_DIR)/%.o,$(SRC_EVCC_APP))

OBJ_SECC_LIB 		+= $(patsubst %.c,$(OBJ_SECC_DIR)/%.o,$(SRC_SECC_LIB))
OBJ_SECC_APP 		+= $(patsubst %.c,$(OBJ_SECC_DIR)/%.o,$(SRC_SECC_APP))

# target
TARGET_EVCC_LIB		:= ./evcc/libevcc.a
TARGET_EVCC_APP		:= ./evcc/evcc.exe

TARGET_SECC_LIB		:= ./secc/libsecc.a
TARGET_SECC_APP		:= ./secc/secc.exe

# make
.PHONY: evcc_lib evcc_app secc_lib secc_app

all: evcc_lib evcc_app secc_lib secc_app
	@echo -e "\033[32;40mAll Done ... \033[0m"

evcc_lib: $(TARGET_EVCC_LIB)
	@echo -e "\033[32;40m"
	@file $^
	@ls -l $^
	@echo -e "\033[0m"

$(TARGET_EVCC_LIB): $(OBJ_EVCC_LIB)
	@echo -e "\033[32;40m[AR]$@ \033[0m"
	@${AR} -cr $@ $^

evcc_app: $(TARGET_EVCC_APP)
	@echo -e "\033[32;40m"
	@file $^
	@ls -l $^
	@echo -e "\033[0m"

$(TARGET_EVCC_APP): $(OBJ_EVCC_APP) $(TARGET_EVCC_LIB)
	@echo -e "\033[32;40m[CC]$@ \033[0m"
	@$(CC) $(CFLAGS) -o $@ $^



secc_lib: $(TARGET_SECC_LIB)
	@echo -e "\033[32;40m"
	@file $^
	@ls -l $^
	@echo -e "\033[0m"

$(TARGET_SECC_LIB): $(OBJ_SECC_LIB)
	@echo -e "\033[32;40m[AR]$@ \033[0m"
	@${AR} -cr $@ $^

secc_app: $(TARGET_SECC_APP)
	@echo -e "\033[32;40m"
	@file $^
	@ls -l $^
	@echo -e "\033[0m"

$(TARGET_SECC_APP): $(OBJ_SECC_APP) $(TARGET_SECC_LIB)
	@echo -e "\033[32;40m[CC]$@ \033[0m"
	@$(CC) $(CFLAGS) -o $@ $^

$(OBJ_EVCC_DIR)/%.o:%.c $(INC_FILE_EVCC_APP)
	@echo -e "\033[32;40m[CC]$< \033[0m"
	@mkdir -p $(dir $@)
	@$(CC) -c $(CFLAGS) $(INC_EVCC) -o $@ $<

$(OBJ_SECC_DIR)/%.o:%.c $(INC_FILE_SECC_APP)
	@echo -e "\033[32;40m[CC]$< \033[0m"
	@mkdir -p $(dir $@)
	@$(CC) -c $(CFLAGS) $(INC_SECC) -o $@ $<

clean:
	@rm -f $(TARGET_EVCC_LIB)
	@rm -f $(TARGET_EVCC_APP)
	@rm -f $(TARGET_SECC_LIB)
	@rm -f $(TARGET_SECC_APP)
	@rm -rf $(OBJ_EVCC_DIR)
	@rm -rf $(OBJ_SECC_DIR)
	@echo -e "\033[32;40mAll Clean ... \033[0m"

